Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

co

Package Overview
Dependencies
Maintainers
3
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

co

generator async control flow goodness

  • 4.6.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27M
increased by14.25%
Maintainers
3
Weekly downloads
 
Created

What is co?

The co npm package is a generator based flow-control utility for Node.js and the browser, making it easier to work with asynchronous JavaScript operations. It allows you to use generators to yield any function that returns a Promise. It can be used to simplify callback or promise-based code, especially in the context of async/await patterns.

What are co's main functionalities?

Sequential Execution

This feature allows for sequential execution of asynchronous tasks. The code sample demonstrates how you can use co to run promises in sequence using a generator function, which yields a promise that resolves to true.

co(function* () {
  var result = yield Promise.resolve(true);
  return result;
}).then(function (value) {
  console.log(value);
}, function (err) {
  console.error(err.stack);
});

Error Handling

This feature demonstrates how co can be used for error handling in asynchronous operations. The code sample shows a generator function yielding a promise that gets rejected, and the error is caught and logged.

co(function* () {
  try {
    yield Promise.reject(new Error('Oops!'));
  } catch (err) {
    console.error(err.message);
  }
});

Parallel Execution

This feature showcases how co can handle parallel execution of promises. The code sample illustrates a generator function yielding an array of promises, which co runs in parallel, and then logs the array of results.

co(function* () {
  var res = yield [
    Promise.resolve(1),
    Promise.resolve(2),
  ];
  return res;
}).then(function (value) {
  console.log(value);
});

Other packages similar to co

Keywords

FAQs

Package last updated on 09 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc